﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace DtsExplorerProject.Helpers
{
    public class DTSDataGridView : DataGridView
    {
        static public ElementEditorForm EditorForm;
        int position;
        string path;
        string valuetype;
        string configuredtype;
        string configuredvalue;

        public DTSDataGridView()
        {
            EditorForm = new ElementEditorForm();
        }
        
        protected override void OnRowPostPaint(DataGridViewRowPostPaintEventArgs arg)
        {
            base.OnRowPostPaint(arg);
            string strRowNumber = (arg.RowIndex + 1).ToString();
            while (strRowNumber.Length < this.RowCount.ToString().Length) strRowNumber = "0" + strRowNumber;
            SizeF size = arg.Graphics.MeasureString(strRowNumber, this.Font);
            if (this.RowHeadersWidth < (int)(size.Width + 20)) this.RowHeadersWidth = (int)(size.Width + 20);
            Brush b = SystemBrushes.ControlText;
            arg.Graphics.DrawString(strRowNumber,
                this.Font, b,
                arg.RowBounds.Location.X + 15, 
                arg.RowBounds.Location.Y + ((arg.RowBounds.Height - size.Height) / 2));
            base.OnRowPostPaint(arg);            
        }
        
        protected override void OnCellBeginEdit(DataGridViewCellCancelEventArgs arg)
        {
            base.OnCellBeginEdit(arg);
            position = arg.RowIndex;
            path = this[0, arg.RowIndex].Value.ToString();
            valuetype = this[1, arg.RowIndex].Value.ToString();
            configuredtype = this[2, arg.RowIndex].Value.ToString();
            configuredvalue = this[3, arg.RowIndex].Value.ToString();
            EditorForm = new ElementEditorForm();
            EditorForm.Load+=new EventHandler(EditorForm_Load);
            EditorForm.ShowDialog();
            EditorForm.ShowInTaskbar = false;     
        }

        private void EditorForm_Load(object sender, EventArgs args)
        {  
            EditorForm.Rownumber = ++position;
            EditorForm.PathValue = path;
            EditorForm.ValueTypeValue = valuetype;
            EditorForm.ConfiguredTypeValue = configuredtype;
            EditorForm.ConfiguredValue = configuredvalue;
        }


       

        protected override void OnCellEndEdit(DataGridViewCellEventArgs arg)
        {
            base.OnCellEndEdit(arg);

            //if (EditorForm.Disposing)
            //{
            //    this[3, arg.RowIndex].Value = EditorForm.ConfiguredValue;
            //}
        } 
        protected override void OnCellValidated(DataGridViewCellEventArgs arg)
        {
            base.OnCellValidated(arg);
        }
        protected override void OnCellValidating(DataGridViewCellValidatingEventArgs arg)
        {
            base.OnCellValidating(arg);
        }
        protected override void OnCellValueChanged(DataGridViewCellEventArgs arg)
        {
            base.OnCellValueChanged(arg);
        }
        protected override void OnRowLeave(DataGridViewCellEventArgs e)
        {
            base.OnRowLeave(e);
        }
        protected override void OnSelectionChanged(EventArgs e)
        {
       
        }
        protected override void OnUserAddedRow(DataGridViewRowEventArgs e)
        {
            base.OnUserAddedRow(e);
        }
        protected override void OnUserDeletedRow(DataGridViewRowEventArgs e)
        {
            base.OnUserDeletedRow(e);
        }
    }
}
